package com.xiaobai.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.xiaobai.feign.ProductFeignClient;
import com.xiaobai.order.bean.Order;
import com.xiaobai.product.bean.Product;
import com.xiaobai.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    public RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    ProductFeignClient productFeignClient;

    @Override
    public Order getOrderById(Long productId, Long userId) {
        Order order = new Order();
        order.setOrderId(1L);
        Product product = getProductByRPCWithLoadBalanceByAnnotation(productId);
        order.setTotalAmount(product.getPrice().multiply(product.getPrice()));
        order.setUserId(userId);
        order.setAddress("广州");
        order.setProductList(Collections.singletonList(product));
        return order;
    }

    @SentinelResource(value = "createOrder", blockHandler = "createOrderFallBack")
    @Override
    public Order getOrderByFeignId(Long productId, Long userId) {
        Order order = new Order();
        order.setOrderId(1L);
        Product product = productFeignClient.getProductById(productId);
        order.setTotalAmount(product.getPrice().multiply(product.getPrice()));
        order.setUserId(userId);
        order.setAddress("广州");
        order.setProductList(Collections.singletonList(product));
        return order;
    }

    //兜底方案
    public Order createOrderFallBack(Long orderId, Long userId, BlockException e) {
        Order order = new Order();
        order.setOrderId(0L);
        order.setTotalAmount(new BigDecimal("0"));
        order.setUserId(0L);
        order.setAddress("异常信息" + e.getClass());
        return order;
    }

    private Product getProductByRPC(Long orderId) {
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        ServiceInstance instance = instances.get(0);
        String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/product/" + orderId;
        log.info("构造的url为,{}", url);
        Product product = restTemplate.getForObject(url, Product.class);
        log.info("获取到的product为,{}", product);
        return product;
    }

    private Product getProductByRPCWithLoadBalance(Long orderId) {
        //负载均衡
        ServiceInstance instance = loadBalancerClient.choose("service-product");
        String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/product/" + orderId;
        log.info("构造的url为,{}", url);
        Product product = restTemplate.getForObject(url, Product.class);
        log.info("获取到的product为,{}", product);
        return product;
    }

    private Product getProductByRPCWithLoadBalanceByAnnotation(Long orderId) {
        String url = "http://service-product/product/" + orderId;
        log.info("构造的url为,{}", url);
        Product product = restTemplate.getForObject(url, Product.class);
        log.info("获取到的product为,{}", product);
        return product;
    }
}
